/**************************************************************
 *
 * Copyright (c) 2008, ******, All Rights Reserved.
 *
 * 名 称:
 *		base_order_trans.cpp
 * 位 置:
 *		src/biz/base_order_trans.cpp
 * 描 述:
 *		基础交易委托类
 * 备 注:
 *		无
 * 修 改:
 *		2008-09-21  Simon   - 创建
 *
 **************************************************************/

#include <string>
#include <iostream>
#include <exception>
#include "occi.h"
#include "pub/def.h"
#include "pub/str.h"
#include "pub/log.h"
#include "pub/util.h"
#include "pub/db.h"
#include "biz/base_order.h"
#include "biz/base_order_trans.h"

using namespace std;
using namespace oracle::occi;

//----------------------------------------------
//  构造函数
//----------------------------------------------
BaseOrderTrans::BaseOrderTrans()
{
    shareClass		= "0";
}

//----------------------------------------------
//  析构函数
//----------------------------------------------
BaseOrderTrans::~BaseOrderTrans()
{
}

//----------------------------------------------
//  检查输入参数
//----------------------------------------------
void BaseOrderTrans::checkData()
{
	BaseOrder::checkData();

	//检查基金代码
	if (!serviceCode.match("^[0-9]{6}"))
	{
		Log::throwError("基金代码不合法");
	}
}

//----------------------------------------------
//  初始化
//----------------------------------------------
void BaseOrderTrans::init()
{
	int count = 0;

	BaseOrder::init();

	Statement* stmt = conn->createStatement();
	ResultSet* rs = 0;
	try
	{
		//检查基金代码是否存在
		stmt->setSQL(" \
			select count(*) \
			from Fund \
			where fundCode=:fundCode");
		stmt->setString(1, fundCode);
		rs = stmt->executeQuery();
		rs->next();
		count = rs->getInt(1);
		if (count <= 0)
		{
			Log::throwError("基金代码不存在");
		}

		//获取基金状态、基金类型、基金份额类别、TA代码、币种
		stmt->setSQL(" \
			select fundStatus, fundType, fundShareClass, currency, taCode \
			from Fund \
			where fundCode = fundCode");
		stmt->setString(1, fundCode);
		rs = stmt->executeQuery();
		rs->next();
		fundStatus = rs->getString(1);
		fundType = rs->getString(2);
		fundShareClass = rs->getString(3);
		currency = rs->getString(4);
		taCode = rs->getString(5);

		//修改基金份额类别
		if (fundShareClass == "0")
		{
			shareClass = "0";
		}
		else if (fundShareClass == "1")
		{
			shareClass = "1";
		}

		//检查基金帐号是否存在
		stmt->setSQL(" \
			select count(*) \
			from AcctTA \
			where transAccountID=:transAccountID \
			and taCode = :taCode \
			and taAcctStatus not in ('1')");
		stmt->setString(1, transAccountID);
		stmt->setString(2, taCode);
		rs = stmt->executeQuery();
		rs->next();
		count = rs->getInt(1);
		if (count <= 0)
		{
			Log::throwError("基金帐号不存在");
		}

		//获取基金帐号、基金帐号状态
		stmt->setSQL(" \
			select taAccountID, taAcctStatus \
			from AcctTA \
			where transAccountID = :transAccountID \
			and taCode = :taCode \
			and taAcctStatus not in ('1')");
		stmt->setString(1, transAccountID);
		stmt->setString(2, taCode);
		rs = stmt->executeQuery();
		rs->next();
		taAccountID = rs->getString(1);
		taAcctStatus = rs->getString(2);

		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
	}
	catch (...)
	{
		stmt->closeResultSet(rs);
		conn->terminateStatement(stmt);
		throw ;
	}
}

//----------------------------------------------
//  检查功能权限
//----------------------------------------------
void BaseOrderTrans::checkPriv()
{
	BaseOrder::checkPriv();

	//检查基金账户状态是否允许执行该业务
	priv->serviceCode 	= serviceCode;
	priv->taAcctStatus	= taAcctStatus;
	priv->checkAcctTA();

	//检查基金状态是否允许执行该业务
	priv->serviceCode	= serviceCode;
	priv->fundStatus	= fundStatus;
	priv->checkFund();
}
